package cn.com.se.多线程.queue;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Description TODO
 * @Author:hu.rl
 * @Date:2019-06-05 14:22
 * @Version 1.0
 **/
public class QueueTest {

    public static final LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(100);

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 200; i++) {
                    try {
                        System.out.println("线程1加入队列" + i);
                        linkedBlockingQueue.put(i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 200; i++) {
                    try {
                        System.out.println("线程2加入队列" + i);
                        linkedBlockingQueue.put(i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

     new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 200; i++) {
                    try {
                        System.out.println("取出任务:" + linkedBlockingQueue.take());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
